﻿#region Imports
using System;
using System.IO;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
#endregion

namespace SqlMetalBuilder
{
    public partial class MDIParent : Form
    {
        private int childFormNumber = 0;

        public MDIParent()
        {
            InitializeComponent();
        }
        public MDIParent(string fileName)
        {
            InitializeComponent();
            if (fileName != null)
            {
                MDIChildProject childForm = new MDIChildProject();
                childForm.MdiParent = this;
                //childForm.Text = "Untitled Project " + childFormNumber++;
                childForm.Show();
                childForm.LoadProject(fileName);
            }
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            MDIChildProject childForm = new MDIChildProject();
            childForm.MdiParent = this;
            childForm.Text = "Untitled Project " + childFormNumber++;
            childForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "SqlMetal Builder Project (*.smb)|*.smb";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string strFileName = openFileDialog.FileName;
                MDIChildProject childForm = new MDIChildProject();
                childForm.MdiParent = this;
                //childForm.Text = "Untitled Project " + childFormNumber++;
                childForm.Show();
                childForm.LoadProject(strFileName);

            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void contentsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("To be done");
        }

        private void indexToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("To be done");
        }

        private void searchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("To be done");
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox objAboutBox = new AboutBox();
            objAboutBox.ShowDialog();
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Options objOptions = new Options();
            //objOptions.MdiParent = this;
            objOptions.ShowDialog();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("To be done");
        }

        private void MDIParent_Load(object sender, EventArgs e)
        {
        }

        protected override void OnMdiChildActivate(EventArgs e)
        {

            base.OnMdiChildActivate(e); //REQUIRED 
            HandleChildMerge(); 
        }
        private void HandleChildMerge()
        {
            ToolStripManager.RevertMerge(toolStrip);
            MDIChildProject ChildForm = this.ActiveMdiChild as MDIChildProject;
            if (ChildForm != null)
            {
                ToolStripManager.Merge(ChildForm.ToolStrip, toolStrip);
            }
        }

        public string StatusText
        {
            set {
                this.toolStripStatusLabel.Text = value;
            }
        }
    }
}